package com.favorites.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.favorites.common.ParseFavorites;
import com.favorites.vo.TouTiaoBlog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by
 *
 * @author dw
 * @date 2018/04/17
 */
@Controller
public class IndexController {
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
    private static final String CACHE_FAVORITES = "getFavoritesInfo";
    @Autowired
    ParseFavorites parseFavorites;
    @Autowired
    @Qualifier("strKeyRedisTemplate")
    private RedisTemplate redisClient;

    @RequestMapping("/")
    public String home() {
        return "index";
    }

    @GetMapping("/testdy")
    public String testdy(Model model, String type, String size,
                         String startYear, String email) {
        List<TouTiaoBlog> data = new ArrayList<TouTiaoBlog>();
        TouTiaoBlog t = null;

        for (int i = 0; i < 20; i++) {
            t = new TouTiaoBlog();
            t.setBehot_time(DateUtil.date().getTime() + "");
            t.setDisplay_url("baidu.com");
            t.setTitle("abc" + i);
            data.add(t);
        }
        model.addAttribute("data", data);
        return "dy";
    }

    /**
     * 订阅信息_后台模板引擎
     *
     * @param type
     * @param size
     * @param email
     */
    @RequestMapping(value = "/getFavoritesInfo", method = RequestMethod.GET)
    public String getFavoritesInfo(Model model, String type, String size,
                                   String startYear, String email) {
        if (StrUtil.isBlank(startYear)) {
            startYear = "2017";
        }
        Object getFavoritesInfo = redisClient.opsForValue().get(CACHE_FAVORITES + startYear);
        List<TouTiaoBlog> data = null;
        if (null != getFavoritesInfo) {
            data = (List<TouTiaoBlog>) getFavoritesInfo;
            logger.info("从缓存命中:{}大小{}", CACHE_FAVORITES + startYear, data.size());
            redisClient.opsForValue().set(CACHE_FAVORITES + startYear, data, 8, TimeUnit.HOURS);
        } else {
            logger.info("未缓存命中:{}", CACHE_FAVORITES + startYear);
            data = getToutiaoData(type, size, startYear, email);
            redisClient.opsForValue().set(CACHE_FAVORITES + startYear, data);
        }
        model.addAttribute("data", data);
        return "dy";
    }

    @RequestMapping(value = "/remake", method = RequestMethod.GET)
    @ResponseBody
    public String remake(String type, String size,
                         String startYear, String email) {
        if (StrUtil.isBlank(startYear)) {
            startYear = "2017";
        }
        redisClient.delete(CACHE_FAVORITES + startYear);

        List<TouTiaoBlog> data = null;

        logger.info("重新生成缓存:{}", CACHE_FAVORITES + startYear);
        data = getToutiaoData(type, size, startYear, email);
        redisClient.opsForValue().set(CACHE_FAVORITES + startYear, data, 8, TimeUnit.HOURS);

        return "缓存已经重新生成";
    }

    /**
     * 订阅信息_前后台分离返回存json数据
     *
     * @param type
     * @param size
     * @param email
     */
    @RequestMapping(value = "/getFavoritesInfo_json", method = RequestMethod.GET)
    @ResponseBody//返回json数据用,返回模板引擎不需要
    public List<TouTiaoBlog> getFavoritesInfo_json(String type, String size,
                                                   String startYear, String email) {
        List<TouTiaoBlog> data = getToutiaoData(type, size, startYear, email);

        return data;
    }


    private List<TouTiaoBlog> getToutiaoData(String type, String size, String startYear, String email) {
        if (StrUtil.isNotBlank(startYear)) {
            return parseFavorites.parseAndBackJson(startYear);
        }
        return parseFavorites.parseAndBackJson("2017");
    }
}
